package com.jinlejia.erp.ui.mine.contract;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivitContractIntoBinding;
import com.jinlejia.erp.ui.mine.adapter.ContractIntoAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;


/**
 * 金钥匙合同分成（Activity）
 */

public class GoldenKeyIntoActivity extends BaseActivity {
    private ContractIntoAdapter adapter;
    private ArrayList<Map<String, Object>> dataList = new ArrayList<>();

    private ActivitContractIntoBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activit_contract_into);
        initView();
    }


    /**
     * 初始化视图
     */
    private void initView() {
        getData();
        binding.contractIntoRecyclerView.setHasFixedSize(true);
        binding.backBtn.setOnClickListener(view -> finish());
        binding.contractIntoRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ContractIntoAdapter(dataList);
        binding.contractIntoRecyclerView.setAdapter(adapter);
    }


    /**
     * 获取数据
     */
    private void getData() {
        OkHttpUtils
                .get()
                .url(ApiConfig.GOLDEN_KEY_INTO)
                .addParams("pactCode", getIntent().getStringExtra("pactCode"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(GoldenKeyIntoActivity.this, response, adapter)) {
                            ArrayList<Map<String, Object>> list2 = JSON.parseObject(response.get("data").toString(),
                                    new TypeReference<ArrayList<Map<String, Object>>>() {
                                    });
                            for (int i = 0; i < list2.size(); i++) {
                                dataList.add(list2.get(i));
                            }
                            adapter.setData(dataList);
                        }
                    }
                });
    }

}
